package com.gxl.desigin;

import com.gxl.design.abstractfactorypattern.factory.AbstractFactory;
import com.gxl.design.abstractfactorypattern.factory.FactoryKind;
import com.gxl.design.adapterpattern.FatherImpl;
import com.gxl.design.adapterpattern.FatherInterface;
import com.gxl.design.adapterpattern.adapter.AdapterFather;
import com.gxl.design.adapterpattern.adapter.SonImpl;
import com.gxl.design.bridgepattern.MyMake;
import com.gxl.design.bridgepattern.impl.Female;
import com.gxl.design.bridgepattern.impl.Male;
import com.gxl.design.builderpattern.builder.OrderBuilder;
import com.gxl.design.commandpattern.AddOne;
import com.gxl.design.commandpattern.DivideFour;
import com.gxl.design.commandpattern.MultiplyThree;
import com.gxl.design.commandpattern.SubtractTwo;
import com.gxl.design.commandpattern.order.Orders;
import com.gxl.design.compositepattern.Area;
import com.gxl.design.decoratorpattern.Animal;
import com.gxl.design.decoratorpattern.Pander;
import com.gxl.design.decoratorpattern.Tiger;
import com.gxl.design.decoratorpattern.decoretor.DecoratorAnimalImpl;
import com.gxl.design.factorypattern.factory.SparePartsFactory;
import com.gxl.design.factorypattern.task.SpareParts;
import com.gxl.design.filterpattern.Criteria;
import com.gxl.design.filterpattern.impl.AudiCriteria;
import com.gxl.design.filterpattern.impl.OrCriteria;
import com.gxl.design.filterpattern.impl.RunCarCriteria;
import com.gxl.design.filterpattern.impl.USACriteria;
import com.gxl.design.mementopattern.CareTaker;
import com.gxl.design.mementopattern.Original;
import com.gxl.design.observerpattern.Bill;
import com.gxl.design.observerpattern.observer.EmailMessge;
import com.gxl.design.observerpattern.observer.ShortMessage;
import com.gxl.design.observerpattern.observer.Wechatmessge;
import com.gxl.design.proxypattern.Login;
import com.gxl.design.proxypattern.proxy.ProxyLogin;
import com.gxl.design.singlepattern.SingleClass;
import com.gxl.design.statepattern.Context;
import com.gxl.design.statepattern.StartState;
import com.gxl.design.statepattern.StopState;
import com.gxl.design.strategypattern.Content;
import com.gxl.design.strategypattern.strategy.CashEquivalent;
import com.gxl.design.strategypattern.strategy.Discount;
import com.gxl.design.strategypattern.strategy.RedPage;
import com.gxl.design.templatepattern.Car;
import com.gxl.design.templatepattern.Robot;
import com.gxl.design.templatepattern.Run;
import org.junit.Test;

import java.util.ArrayList;
import java.util.List;

/**
 * @author guoxiaolin
 * @description
 * @since 2019/4/02 15:10
 */
public class test {

    /**
     * 单例模式测试
     */
    @Test
    public void getSingleInstance(){
        SingleClass.getInstance().printString();
    }

    /**
     * 工厂模式测试
     */
    @Test
    public void produceMirror(){
        SparePartsFactory sparePartsFactory = new SparePartsFactory();
        SpareParts spareParts = sparePartsFactory.production("mirror");
        System.out.println(spareParts.production());
    }

    /**
     * 抽象工厂模式测试
     */
    @Test
    public void abstractProduceMirror(){

        AbstractFactory mirrorFactory = FactoryKind.getFactory("mirror");
        System.out.println(mirrorFactory.getMirrorProducer("A").getMirror());
        System.out.println(mirrorFactory.getMirrorProducer("B").getMirror());

        AbstractFactory glassFactory =  FactoryKind.getFactory("glass");
        System.out.println(glassFactory.getGlassProducer("A").getGlass());
        System.out.println(glassFactory.getGlassProducer("B").getGlass());
        System.out.println(glassFactory.getGlassProducer("C").getGlass());
    }

    /**
     * 适配器模式测试
     */
    @Test
    public void testAdapter(){

        FatherInterface fatherInterface = new FatherImpl();
        fatherInterface.eat();

        AdapterFather adapterFather = new SonImpl();
        adapterFather.run();
        adapterFather.eat();
    }

    /**
     * 建造者模式测试
     */
    @Test
    public void testBuilder(){

        OrderBuilder orderBuilder = new OrderBuilder();

        orderBuilder.buildAOrder();
        System.out.println();
        orderBuilder.buildBOrder();

    }

    /**
     * 观察者模式测试
     */
    @Test
    public void testObserver(){

        Bill bill =  new Bill();

        new Wechatmessge(bill);
        new EmailMessge(bill);
        new ShortMessage(bill);

        bill.generatedBill(6999);
    }

    /**
     * 策略模式测试
     */
    @Test
    public void testStragegy(){

        Content content1 = new Content(new RedPage(),509);

        Content content2 = new Content(new CashEquivalent(),509);

        Content content3 = new Content(new Discount(),509);
    }

    /**
     * 代理模式测试
     */
    @Test
    public void testProxy(){

        Login login = new ProxyLogin();

        login.login("1","1");
    }

    /**
     * 组合模式测试
     */
    @Test
    public void testComposite(){

        Area contry = new Area("中国", 0);

        Area gd = new Area("广东省", 1);
        Area hb = new Area("河北省", 1);

        Area gz = new Area("广州市", 2);
        Area sz = new Area("深圳市", 2);

        Area bj = new Area("北京市", 2);
        Area sjz = new Area("石家庄市", 2);

        contry.add(gd).add(hb);

        gd.add(gz).add(sz);
        hb.add(bj).add(sjz);

        //打印该结构
        System.out.println(contry);
        for (Area c : contry.getContries()) {
            System.out.println(c);
            for (Area c1 : c.getContries()) {
                System.out.println(c1);
            }
        }
    }
    /**
     * 摸板模式测试
     */
    @Test
    public void testTemplate(){

        Run c = new Car();
        c.ok();
        c = new Robot();
        c.ok();
    }


    /**
     * 装饰器模式测试
     */
    @Test
    public void testDecorator(){

        Animal animal = new Pander();
        animal.animalName();
        System.out.println("-");

        animal = new DecoratorAnimalImpl(new Tiger());
        animal.animalName();
        System.out.println("-");

        animal = new DecoratorAnimalImpl(new Pander());
        animal.animalName();
        System.out.println("-");
    }

    /**
     * 命令模式测试
     */
    @Test
    public void testCommand(){

        Orders orders = new Orders();
        new AddOne(orders);
        new MultiplyThree(orders);
        new DivideFour(orders);
        new SubtractTwo(orders);

        orders.excuteOrders();

    }

    /**
     * 状态模式测试
     */
    @Test
    public void testState(){

        Context context = new Context();

        StartState startState = new StartState();
        startState.doAction(context);
        System.out.println(context.getState().showState());

        StopState stopState = new StopState();
        stopState.doAction(context);
        System.out.println(context.getState().showState());

    }

    /**
     * 过滤器模式
     */
    @Test
    public void testCriteria(){

        List<com.gxl.design.filterpattern.Car> cars = new ArrayList<>();

        cars.add(new com.gxl.design.filterpattern.Car("Audi","RunCar","USA"));
        cars.add(new com.gxl.design.filterpattern.Car("BMW","Car","UK"));
        cars.add(new com.gxl.design.filterpattern.Car("lsls","Car","USA"));
        cars.add(new com.gxl.design.filterpattern.Car("lsls","RunCar","USA"));
        cars.add(new com.gxl.design.filterpattern.Car("Ben","Car","UK"));
        cars.add(new com.gxl.design.filterpattern.Car("Ben","Car","CN"));
        cars.add(new com.gxl.design.filterpattern.Car("Audi","RunCar","USA"));

        Criteria usaCriteria = new USACriteria();
        Criteria AudiCriteria = new AudiCriteria();
        Criteria RunCarteria = new RunCarCriteria();
        Criteria orCriteria = new OrCriteria(AudiCriteria,RunCarteria,usaCriteria);

        List<com.gxl.design.filterpattern.Car> cass = orCriteria.meetCriteria(cars);

        for (com.gxl.design.filterpattern.Car c : cass) {
            System.out.println("Person : [ Name : " + c.getName()
                    +", Contry : " + c.getContry()
                    +", Type : " + c.getType()
                    +" ]");
        }
    }

    /**
     * 桥接模式测试
     */
    @Test
    public void testBridge(){

        MyMake myMake = new MyMake(170,123,new Male());
        myMake.makePeople();

        MyMake myMake2 = new MyMake(170,123,new Female());
        myMake2.makePeople();

    }


    /**
     * 备忘录模式测试
     */
    @Test
    public void testMemento(){

        Original original = new Original();
        CareTaker careTaker = new CareTaker();

        original.setText("备忘录 1");
        careTaker.add(original.saveText());

        original.setText("备忘录 2");
        careTaker.add(original.saveText());

        original.setText("备忘录 3");

        System.out.println("current text : " + original.getText());
        original.rollText(careTaker.get(0));
        System.out.println("first text : " + original.getText());
        original.rollText(careTaker.get(1));
        System.out.println("second text : " + original.getText());


    }

}
